package top.withwings.bonus;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.util.DateUtils;
import top.withwings.bonus.bonus.pojo.BonusForm;
import top.withwings.bonus.bonus.pojo.Good;
import top.withwings.bonus.bonus.service.BonusFormService;
import top.withwings.bonus.bonus.service.GoodService;
import top.withwings.bonus.common.pojo.PageParam;
import top.withwings.bonus.common.pojo.User;
import top.withwings.bonus.common.service.UserService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class BonusFormInit {

    List<Good> goods = new ArrayList<>();
    List<Good> fruits = new ArrayList<>();
    List<Good> foods = new ArrayList<>();
    List<Good> normals = new ArrayList<>();
    List<Good> clothes = new ArrayList<>();

    List<User> salesmen = new ArrayList<>();


    List<Date> dates = new ArrayList<>();


    @Autowired
    GoodService goodService;

    @Autowired
    UserService userService;

    @Autowired
    BonusFormService bonusFormService;

    @Before
    public void init(){

        this.goods = goodService.searchGood(new PageParam(0,100),"",false);

        for (Good good : goods) {
            if(good.getId() <=7){
                normals.add(good);
            }else if(good.getId() <=13){
                clothes.add(good);
            }else if(good.getId()<=17){
                fruits.add(good);
            }else{
                foods.add(good);
            }


        }


        List<User> users = userService.findAll();
        for (User user : users) {
            if(user.isAdmin()) continue;
            salesmen.add(user);
        }

        initDates();

    }

    @Test
    public void saveForm(){

        for (Date date : this.dates) {

            int formCount = RandomUtils.random(66);
            for (int i = 0; i < formCount; i++) {

                BonusForm form = new BonusForm();
                form.setSalesman(RandomUtils.random(this.salesmen));
                form.setGood(RandomUtils.random(this.goods));
                form.setEffectiveDate(date);
                form.setPrice(simPrice(form.getGood()));
                form.setQuantity(simQuantity(form.getGood()));
                form.setRemark(simRemark());
                form.setMakeUp(simMakeUp());
                form.setBatchNo(simBatchNo(form.getGood()));
                form.setExpiration(simExpiration(form.getGood(),date));
                bonusFormService.create(form);

            }


        }



    }

    private String simBatchNo(Good good) {
        return good.getCode().substring(0,3)+RandomUtils.random("0203","0303","8888","9032","8185");

    }

    private Date simExpiration(Good good, Date date) {
        int days;
        if(foods.contains(good)){
            days = RandomUtils.random(1,180);

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_YEAR, days);
            return calendar.getTime();

        }else if(fruits.contains(good)){
            days = RandomUtils.random(1,30);

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_YEAR, days);
            return calendar.getTime();
        }else {
            return null;
        }
    }


    private double simMakeUp() {
        if(RandomUtils.randomBoolean(3)){
            return RandomUtils.random(1,8);
        }else{
            return 0;
        }
    }

    private String simRemark() {
        if(RandomUtils.randomBoolean(12)){
            return "测试内容"+RandomUtils.randomText(10);
        }else{
            return "";
        }
    }

    private double simQuantity(Good good) {
        if(good.getStandardPrice() > 1000){
            return RandomUtils.random(1,3);
        }else if(fruits.contains(good)){
            return RandomUtils.random(new Double[]{1.0, 2.0,3.0,4.0,2.2,3.8,4.5 ,8.8});
        }else{
            return RandomUtils.random(1,30);
        }
    }

    private double simPrice(Good good) {



        if(clothes.contains(good)){
            if(RandomUtils.randomBoolean(25)){
                return good.getStandardPrice();
            }else{
                BigDecimal bg = new BigDecimal(good.getStandardPrice()* RandomUtils.random(0.9, 1.8));
                return  bg.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
            }
        }else{
            if(RandomUtils.randomBoolean(65)){
                return good.getStandardPrice();
            }else{
                BigDecimal bg = new BigDecimal(good.getStandardPrice()* RandomUtils.random(0.9, 1.2));
                return  bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            }
        }
    }


    private void initDates() {

        Calendar start = DateUtils.create(2018,01,01);
        Calendar end = DateUtils.createToday();

        for (Calendar i = start; i.before(end) ; i.add(Calendar.DAY_OF_YEAR,1)) {
            this.dates.add(i.getTime());
        }



    }


}
